java实现发送短信(亲测可用)

您所在的位置:网站首页 短信发送平台 免费下载 java实现发送短信(亲测可用)

java实现发送短信(亲测可用)

2024-05-31 16:01| 来源: 网络整理| 查看: 265

下面我是用秒嘀科技的短信发送平台,我们注册一下免费赠送10元,可以发送大约200条短信,这个是官网大家可以注册一下http://www.miaodiyun.com/?qmymark=dedcbe590015c3e82cc9bbfc7f3a1e74

 

 

然后设置一下发送短信的模板

 

 

 

注意:  我们一会发送短信的签名,和内容必须和你设置的短信模板 中的短信签名 短信内容要保持一致(短信内容在编辑里面可以看到)

 

这是我的文件目录

 

                                                      

 

 

TextDemo.java

 

public class TextDemo { public static void main(String [] args){ execute(); } private static String operation = "/industrySMS/sendSMS"; private static String accountSid = Config.ACCOUNT_SID; private static String to = "18337709941,15617969625"; private static String smsContent = "【嘉佳科技有限公司】您的验证码为{1},请于{2}分钟内正确输入,如非本人操作,请忽略此短信。"; // /** * 验证码通知短信 */ public static void execute() { String tmpSmsContent = null; try{ tmpSmsContent = URLEncoder.encode(getSmsContent(smsContent), "UTF-8"); }catch(Exception e){ } String url = Config.BASE_URL + operation; String body = "accountSid=" + accountSid + "&to=" + to + "&smsContent=" + tmpSmsContent + HttpUtil.createCommonParam(); // 提交请求 String result = HttpUtil.post(url, body); System.out.println("result:" + System.lineSeparator() + result); } private static String getSmsContent(String smsContent){ HashMap map = new HashMap(); map.put("key","123456"); map.put("key1","5"); return MessageFormat.format(smsContent,map.get("key"),map.get("key"),map.get("key1")); } }

 

 

Config.java

 

public class Config { /** * url前半部分 */ public static final String BASE_URL = "https://api.miaodiyun.com/20150822"; /** * 开发者注册后系统自动生成的账号,可在官网登录后查看 */ public static final String ACCOUNT_SID = "455f34bxxxxxxxxxxxxxxxxdbb968"; /** * 开发者注册后系统自动生成的TOKEN,可在官网登录后查看 */ public static final String AUTH_TOKEN = "918a7b8xxxxxxxxxxxx9a5021"; /** * 响应数据类型, JSON或XML */ public static final String RESP_DATA_TYPE = "json"; }

 

 

HttpUtil.java

 

public class HttpUtil { /** * 构造通用参数timestamp、sig和respDataType * * @return */ public static String createCommonParam() { // 时间戳 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String timestamp = sdf.format(new Date()); // 签名 String sig = DigestUtils.md5Hex(Config.ACCOUNT_SID + Config.AUTH_TOKEN + timestamp); return "×tamp=" + timestamp + "&sig=" + sig + "&respDataType=" + Config.RESP_DATA_TYPE; } /** * post请求 * * @param url * 功能和操作 * @param body * 要post的数据 * @return * @throws IOException */ public static String post(String url, String body) { System.out.println("url:" + System.lineSeparator() + url); System.out.println("body:" + System.lineSeparator() + body); String result = ""; try { OutputStreamWriter out = null; BufferedReader in = null; URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); // 设置连接参数 conn.setDoOutput(true); conn.setDoInput(true); conn.setConnectTimeout(5000); conn.setReadTimeout(20000); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 提交数据 out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); out.write(body); out.flush(); // 读取返回数据 in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String line = ""; boolean firstLine = true; // 读第一行不加换行符 while ((line = in.readLine()) != null) { if (firstLine) { firstLine = false; } else { result += System.lineSeparator(); } result += line; } } catch (Exception e) { e.printStackTrace(); } return result; } /** * 回调测试工具方法 * * @param url * @param reqStr * @return */ public static String postHuiDiao(String url, String body) { String result = ""; try { OutputStreamWriter out = null; BufferedReader in = null; URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); // 设置连接参数 conn.setDoOutput(true); conn.setDoInput(true); conn.setConnectTimeout(5000); conn.setReadTimeout(20000); // 提交数据 out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); out.write(body); out.flush(); // 读取返回数据 in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String line = ""; boolean firstLine = true; // 读第一行不加换行符 while ((line = in.readLine()) != null) { if (firstLine) { firstLine = false; } else { result += System.lineSeparator(); } result += line; } } catch (Exception e) { e.printStackTrace(); } return result; } }

 

再发送短信的时候,遇到了几个问题,在这里我做一下记录.

 

第一个就是 返回错误码 00134“ 没有和内容匹配的模板”

 

这个问题在于

 

 

 

还有一个就是短信内容{1}{2}我是用MessageFormat进行的格式化处理

 

        

 

具体可以看我的博客 https://blog.csdn.net/qq_40646143/article/details/81707231

 

还有一个问题就是请求地址的错误,我们应该根据秒嘀的API来写

 

https://api.miaodiyun.com/20150822/industrySMS/sendSMS

 

秒嘀的Https api地址是:http://www.miaodiyun.com/doc/https_account.html

 

秒嘀还提供了DEMO下载,我们也可以下载demo来进行测试,地址是http://www.miaodiyun.com/doc/demo.html

 

我们也可以根据自己的debug的状态码,来查找对应的错误,地址是:http://www.miaodiyun.com/doc/status_code.html

 

贴上效果图

 

 

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3